package cn.bugDance.domain.activity.service.armory;

import cn.bugDance.domain.activity.model.entity.ActivityCountEntity;
import cn.bugDance.domain.activity.model.entity.ActivityEntity;
import cn.bugDance.domain.activity.model.entity.ActivitySkuEntity;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.strategy.model.entity.StrategyAwardEntity;
import cn.bugDance.domain.strategy.repository.IStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RaffleActivityArmory implements IRaffleActivityArmory{

    @Resource
    private IActivityRepository activityRepository;
    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public Long assembleRaffleActivity(Long activityId) {
        List<Long> relatedSkus = activityRepository.queryRelatedSkus(activityId);
        ActivityEntity activityEntity = activityRepository.assembleActivity(activityId);
        for (Long relatedSku : relatedSkus) {
            ActivitySkuEntity activitySkuEntity = activityRepository.assembleActivitySku(relatedSku);
            ActivityCountEntity activityCountEntity = activityRepository.assembleActivityCount(activitySkuEntity.getActivityCountId());
            activityRepository.storeActivitySkuStock(relatedSku, activitySkuEntity.getStockCount(), activityEntity.getEndTime());
        }
        return activityEntity.getStrategyId();
    }

    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long activityId) {
        ActivityEntity activityEntity = activityRepository.queryActivityByActivityId(activityId);
        Long strategyId = activityEntity.getStrategyId();
        return strategyRepository.queryStrategyAwardByStrategyId(strategyId);
    }

    @Override
    public Date queryActivityEndTime(Long activityId) {
        ActivityEntity activityEntity = activityRepository.queryActivityByActivityId(activityId);
        return activityEntity.getEndTime();
    }
}
